//
//  UIPickerProvinceCityProtocol.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 28/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "UIPickerProvinceCityProtocol.h"

@interface UIPickerProvinceCityProtocol (){
    NSArray * provinceList;
    NSArray * cityList;
    NSArray * zoneList;
}
@end


@implementation UIPickerProvinceCityProtocol
@synthesize selectedProvince;
@synthesize selectedCity;
@synthesize selectZone;



-(void)setData:(NSArray *)list{
    provinceList = list;
    selectedProvince = [provinceList firstObject];//默认选 中第一个省份
    
    cityList = [selectedProvince objectForKey:@"sublist"];
    selectedCity = [cityList firstObject];
    
    zoneList = [selectedCity objectForKey:@"sublist"];
    selectZone = [zoneList firstObject];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}



- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {//省
        return [provinceList count];
    }else if(component == 1){//市
        return [cityList count];
    }else if(component == 2){//区
        return [zoneList count];
    }
    return 0;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 45;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    NSString * title = nil;
    if (component == 0) {
        
        NSDictionary * dic = [provinceList objectAtIndex:row];
        
        title = [dic objectForKey:@"name"];
    }else if(component == 1){
        if ([cityList count]) {
            NSDictionary * city = [cityList objectAtIndex:row];
            title = [city objectForKey:@"name"];
        }
    }else if(component == 2){
        if (zoneList) {
            NSDictionary * zone = [zoneList objectAtIndex:row];
            title = [zone objectForKey:@"name"];
        }
    }
    UILabel * label = [[UILabel alloc]init];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = UIColorFromRGB(0x333333);
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        selectedProvince = [provinceList objectAtIndex:row];
        cityList = [selectedProvince objectForKey:@"sublist"];
        selectedCity = [cityList firstObject];
        [pickerView reloadComponent:1];
    }else if(component == 1){
        
        selectedCity = [cityList objectAtIndex:row];
        
        zoneList = [selectedCity objectForKey:@"sublist"];
        selectZone = [zoneList firstObject];
        [pickerView reloadComponent:2];
    }else if(component == 2){
        selectZone = [zoneList objectAtIndex:row];
    }
}


-(void)dealloc{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

@end
